package com.hwhhhh.mygrad.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hwhhhh.mygrad.R;

public class ClsEditFragment extends DialogFragment{
    public static final String TITLE = "title";
    public static final String REQUEST_CODE = "request_code";
    public static final String DATA = "data";
    private View rootView, tvPositive, tvNegative;
    private TextView tvTitle;
    private boolean[] clsArr;

    public static ClsEditFragment newInstance(String title, boolean[] clsArr, int requestCode) {
        ClsEditFragment frag = new ClsEditFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putInt(REQUEST_CODE, requestCode);
        args.putBooleanArray(DATA, clsArr);
        frag.setArguments(args);
        return frag;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_cls_edit, container, false);
        initView();
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initView() {
        CheckBox cb_edit_1, cb_edit_2, cb_edit_3, cb_edit_4, cb_edit_5, cb_edit_6, cb_edit_7, cb_edit_8,
                cb_edit_9, cb_edit_10, cb_edit_11, cb_edit_12, cb_edit_13;
        tvTitle = rootView.findViewById(R.id.tv_edit_title);
        tvPositive = rootView.findViewById(R.id.hint_positive);
        tvNegative = rootView.findViewById(R.id.hint_negative);
        cb_edit_1 = rootView.findViewById(R.id.cb_edit_1);
        cb_edit_2 = rootView.findViewById(R.id.cb_edit_2);
        cb_edit_3 = rootView.findViewById(R.id.cb_edit_3);
        cb_edit_4 = rootView.findViewById(R.id.cb_edit_4);
        cb_edit_5 = rootView.findViewById(R.id.cb_edit_5);
        cb_edit_6 = rootView.findViewById(R.id.cb_edit_6);
        cb_edit_7 = rootView.findViewById(R.id.cb_edit_7);
        cb_edit_8 = rootView.findViewById(R.id.cb_edit_8);
        cb_edit_9 = rootView.findViewById(R.id.cb_edit_9);
        cb_edit_10 = rootView.findViewById(R.id.cb_edit_10);
        cb_edit_11 = rootView.findViewById(R.id.cb_edit_11);
        cb_edit_12 = rootView.findViewById(R.id.cb_edit_12);
        cb_edit_13 = rootView.findViewById(R.id.cb_edit_13);
        CheckBox[] checkBoxes = new CheckBox[]{
                cb_edit_1, cb_edit_2, cb_edit_3, cb_edit_4, cb_edit_5, cb_edit_6, cb_edit_7, cb_edit_8,
                cb_edit_9, cb_edit_10, cb_edit_11, cb_edit_12, cb_edit_13
        };
        // 获取数据
        this.clsArr = getArguments().getBooleanArray(DATA);
        int index = 0;
        for (CheckBox checkBox : checkBoxes) {
            checkBox.setChecked(clsArr[index]);
            int finalIndex = index;
            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                clsArr[finalIndex] = isChecked;
            });
            index ++;
        }
    }

    private void initData() {
        String title = getArguments().getString(TITLE);
        int requestCode = getArguments().getInt(REQUEST_CODE);
        tvTitle.setText(title);
        tvPositive.setOnClickListener(v -> {
            Bundle bundle = getArguments();
            bundle.putBooleanArray(DATA, this.clsArr);
            ((HintFragment.HintCallBack)getActivity()).doPositive(requestCode);
        });
        tvNegative.setOnClickListener(v -> {
            ((HintFragment.HintCallBack)getActivity()).doNegative(requestCode);
        });
    }

    @Override
    public void onStart() {
        // 设置对话框的尺寸
        WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(params);
        // 设置背景透明，使背景的圆角显现
        getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
        super.onStart();
    }
}
